package org.smartercontext.local.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import org.smartercontext.contextmger.utilities.GetProfileUtilities;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * Servlet implementation class UpdateProfileInfServlet
 */
@WebServlet("/UpdateProfileInfServlet")
public class UpdateProfileInfServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateProfileInfServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	if(request.getQueryString()!=null){
    		
    		String queryString = request.getQueryString();
    		HttpSession session = request.getSession();
    		
    		if(session.getAttribute("username")!=null){
    			String username = session.getAttribute("username").toString();
    			
    			try {
					
    				Model usermodel = ContextManagerController.getUsermodel(username);
					ContextManagerController.contextManagerController();
					
					String givenName = request.getParameter("gname");
					String gender = request.getParameter("gender");
					//Birth
						String annio = request.getParameter("annio");
						String month = request.getParameter("month");
						String day = request.getParameter("day");
					
					String birth = month+"/"+day+"/"+annio;
					String relstatus = request.getParameter("relstatus");
					String signifOther = request.getParameter("signifo");
					
						//Quantity of JSP emails 
						int emailQuantityJSP = 0;
						String queryCopy = queryString;
						String emailACo = "emailACo";
						while(queryCopy.indexOf(emailACo)>-1){
							queryCopy = queryCopy.substring(queryCopy.indexOf(emailACo)+emailACo.length(), queryCopy.length());
							emailQuantityJSP++;
						}
					
						//Quantity of JAVASCRIPT emails
						int emailQuantiyJAV = 0;
						String queryCopy1 = queryString;
						String emailJAV = "emailJav";
						while(queryCopy1.indexOf(emailJAV)>-1){
							queryCopy1 = queryCopy1.substring(queryCopy1.indexOf(emailJAV)+emailJAV.length(), queryCopy1.length());
							emailQuantiyJAV++;
						}
					
						//Quantity of JSP mobile nums 
						int mobileQuantityJSP = 0;
						String queryCopy2 = queryString;
						String mobileNumAc = "mobileNumAc";
						while(queryCopy2.indexOf(mobileNumAc)>-1){
							queryCopy2 = queryCopy2.substring(queryCopy2.indexOf(mobileNumAc)+mobileNumAc.length(), queryCopy2.length());
							mobileQuantityJSP++;
						}
					
						//Quantity of JAVASCRIPT mobile nums 
						int mobileQuantityJAV = 0;
						String queryCopy3 = queryString;
						String mobileNumJAV = "mobileNumJav";
						while(queryCopy3.indexOf(mobileNumJAV)>-1){
							queryCopy3 = queryCopy3.substring(queryCopy3.indexOf(mobileNumJAV)+mobileNumJAV.length(), queryCopy3.length());
							mobileQuantityJAV++;
						}
						
					//Preferred Location
						String prefPlace = null;
						String prefNeighb = null;
						String prefAddr = null;
						String prefCity = null;
						String prefRegion = null;
						String prefCountry = null;
						String prefLatitude = null;
						String prefLongitude = null;
						String prefZipCode = null;
						prefLongitude = null;
						if(request.getParameter("prefPlace")!=null&&request.getParameter("prefPlace")!=""){
					prefPlace = request.getParameter("prefPlace");
					 prefNeighb = request.getParameter("prefNeighb");
					 prefAddr = request.getParameter("prefAddr");
					 prefCity = request.getParameter("prefCity");
					 prefRegion = request.getParameter("prefRegion");
					 prefCountry = request.getParameter("prefCountry");
					 prefLatitude = request.getParameter("prefLatitude");
					 prefLongitude = request.getParameter("prefLongitude");
					prefZipCode = request.getParameter("prefZip");
						}
						
					
					GetProfileUtilities gpu = new GetProfileUtilities();
					gpu.setGivenName(givenName);
					gpu.setGender(gender);
					if(annio!=""&&month!=""&&day!=""){
						gpu.setBirthday(birth);
					}
					gpu.setRelationshipStatus(relstatus);
					
					if(relstatus.equals("Divorced")||relstatus.equals("Separated")||relstatus.equals("Single")
					||relstatus.equals("Widowed")){
						gpu.setSignificantOther("");
					}else{
						gpu.setSignificantOther(signifOther);
					}
					
					
					List<String>emailAccounts = new ArrayList<>();
										
					//Get JSP emails 
					for(int i=0;i<emailQuantityJSP;i++){
						String parameter = emailACo+i;
						String email = request.getParameter(parameter);
						if(email!=null && email!=""){
							emailAccounts.add(email);
							
						}
					}
					
					//Get JAV emails
					for(int i=0;i<=emailQuantiyJAV;i++){
						String parameter = emailJAV+i;
						String email = request.getParameter(parameter);
						if(email!=null && email!=""){
							emailAccounts.add(email);
							
						}
					}
					
					List<String>mobileNums = new ArrayList<>();
					
					//Get JSP mobileNums
					for(int i=0;i<mobileQuantityJSP;i++){
						String parameter = mobileNumAc+i;
						String num = request.getParameter(parameter);
						if(num!=""&&num!=null){
							mobileNums.add(num);
							
						}
					}
					
					//Get JAV mobileNums
					for(int i=0;i<=mobileQuantityJAV;i++){
						String parameter = mobileNumJAV+i;
						String num = request.getParameter(parameter);
						if(num!=""&&num!=null){
							mobileNums.add(num);
							
						}
					}
					
					String error = null;
					if(prefPlace!=null&&prefCountry!=null&&prefRegion!=null&&prefCity!=null&&
							prefNeighb!=null){
						gpu.setPrefAddres(prefAddr);
						gpu.setPrefPlace(prefPlace);
						gpu.setPrefNeighborhood(prefNeighb);
						gpu.setPrefCity(prefCity);
						gpu.setPrefRegion(prefRegion);
						gpu.setPrefCountry(prefCountry);
						gpu.setPrefLatitude(prefLatitude);
						gpu.setPrefLongitude(prefLongitude);
						gpu.setPrefZipCode(prefZipCode);
					}else{
						error = "Error=4";
					}
					
					gpu.setEmailAccounts(emailAccounts);
					gpu.setMobileNumbers(mobileNums);
					
					
					Model userModelM = gpu.setProfileUserModel(usermodel, username);
					ContextManagerController.integrateContext(userModelM, username);
					
					
					response.sendRedirect("manageContext_Profile.jsp");
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					response.sendRedirect("manageContext_Profile.jsp?Error=1");
				}
    		}else{
    			response.sendRedirect("login.jsp?Err=3");
    		}
    	}
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
